package com.oss.Intercept;

import java.sql.Timestamp;
import java.util.Date;

// import org.joda.time.DateTime;
import com.eova.aop.AopContext;
import com.eova.aop.MetaObjectIntercept;
import com.eova.common.utils.xx;
import com.jfinal.plugin.activerecord.Db;


/*
import com.eova.config.EovaConst;
import com.eova.model.User;
import com.jfinal.core.Controller;

	public List<String> queryMenuCodeByRid(int rid) {
		String sql = "select DISTINCT(b.menu_code) from eova_role_btn rf LEFT JOIN eova_button b on rf.bid = b.id where b.ui = 'query' and rf.rid = ?";
		return Db.use("eova").query(sql, new Object[] { Integer.valueOf(rid) });
	}
	
*/

public class pt_wgyxx extends MetaObjectIntercept {
	public String tableName = "pt_wgyxx";
	public String className = "com.oss.Intercept." + tableName;

	public String runOracleFunction(AopContext ac, String sql) throws Exception {
		String method = "runOracleFunction";
		String prompt = className + "." + method + ">";
		System.out.println(prompt + "start ...");			
		String result = "";
		String pid = InterceptUtil.getUserID(ac);
		
		// String sql = "select get_sql_aj_wgyxx(?) from dual";
		System.out.println(prompt + "sql=[" + sql + "]");
		System.out.println(prompt + "pid=[" + pid + "]");
		result = Db.use("main").query(sql, new Object[] { Integer.valueOf(pid) }).toString();
		
		result = result.substring(1);
		result = result.substring(0, result.length()-1);
		
		
		System.out.println(prompt +  "end.");
		return result;
	}	
	
	@Override
	public void queryBefore(AopContext ac) throws Exception {
		// DIY 查询，名称有值，用名称里面的值来查状态列
		String method = "queryBefore";
		String prompt = className + "." + method + ">";
		System.out.println(prompt + "start ...");	
		
		String user_id = InterceptUtil.getUserID(ac);
		String name = "";

		System.out.println(prompt + "login_id=[" + user_id + "]");
		int i = 0;
		String ts;
		
		if ( (!user_id.equals("1")) && (!user_id.equals("2")) && (!user_id.equals("3")) && (!user_id.equals("4")) && (!user_id.equals("0")) ) {
			System.out.println(prompt +  "不是管理员,需要限制!");
			while (true) {
				name = ac.ctrl.getPara(i);
				System.out.println(prompt +  "name(" + String.valueOf(i) + ")=[" + name + "]");
				if ( name.indexOf("c_" + tableName) != -1 ) {
					ac.condition = " and id = '" + user_id + "'";
					
					ts = runOracleFunction(ac, "select get_sql_aj_wgyxx(?) from dual");
					System.out.println("ts={" + ts + "}");
					ac.condition = ts;
					 
					System.out.println(prompt +  "ac.condition=[" + ac.condition + "]");
					break;
				} else {
					if (xx.isEmpty(name)) {
						break;
					}
				}
		        i++;			
			}
			
		} else {
			System.out.println(prompt +  "是管理员,不需要限制!");
		}
			
		
		/*
		if (!xx.isEmpty(name)) {
			ac.condition = " state = " + name;
		}
		*/
		
		// 同理：想添加自定义查询条件，直接添加元字段
		// 然后配置为 可查询，禁止编辑，禁止新增，然后获取值
		// 利用本AOP方法可自定义查询sql即可
		// 应用场景：like or 子查询 范围查询 外表关联查询...
		System.out.println(prompt +  "end.");
	}

	
	public String updateBefore(AopContext ac) throws Exception {
		String method = "updateBefore";
		String prompt = className + "." + method + ">";
		System.out.println(prompt + "start ...");	
		
		String user_id = InterceptUtil.getUserID(ac);

		ac.record.set("updateowner", user_id);
		ac.record.set("updatetime", new Timestamp(new Date().getTime()));
		System.out.println(prompt + "end.");
		return super.updateBefore(ac);
	}

	public String addBefore(AopContext ac) throws Exception {
		String method = "addBefore";
		String prompt = className + "." + method + ">";
		System.out.println(prompt + "start ...");
		

		String user_id = InterceptUtil.getUserID(ac);
		ac.record.set("insertowner", user_id);
		ac.record.set("inserttime", new Timestamp(new Date().getTime()));
		System.out.println(prompt + "end.");
		return super.addBefore(ac);
	}	
	
}

